﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace WPFInterface.ViewModel
{
    public class HomeViewModel : BaseViewModel
    {
        #region commandes
        private ICommand _logoutCommand;
        private ICommand _addUserCommand;
        private ICommand _deleteUserCommand;
        private ICommand _addPatientCommand;
        private ICommand _deletePatientCommand;
        #endregion

        #region variables
        private ObservableCollection<Dbo.Patient> _listPatients;
        private ObservableCollection<Dbo.User> _listUsers;
        private string _userName;        
        private object _hasRights;      
        private List<Dbo.Observation> _observationPatient;
        private bool _closeSignal;
        ObservableCollection<string> _prescriptionList = null;
        private Dictionary<String, Byte[]> availablePictures = new Dictionary<String,Byte[]>();
        private Byte[] img;

        public Byte[] Img
        {
            get { return img; }
            set
            {
                if (img != value)
                {
                    img = value;
                    OnPropertyChanged("Img");
                }
            }
        }

        public Dictionary<String, Byte[]> AvailablePictures
        {
            get { return availablePictures; }
            set {
                if (availablePictures != value)
                {
                    availablePictures = value;
                    OnPropertyChanged("AvailablePictures");
                }
            }
        }
        #endregion

        #region getter/setter

        public ObservableCollection<string> PrescriptionList
        {
            get { return _prescriptionList; }
            set
            {
                if (_prescriptionList != value)
                {
                    _prescriptionList = value;
                    OnPropertyChanged("PrescriptionList");
                }
            }
        }

        public List<Dbo.Observation> ObservationPatient
        {
            get { return _observationPatient; }
            set {
                if (_observationPatient != value)
                    {
                        _observationPatient = value;
                        OnPropertyChanged("ObservationPatient");
                    }
            }
        }

        public ObservableCollection<Dbo.Patient> ListPatients
        {
            get { return _listPatients; }
             set
            {
                if (_listPatients != value)
                {
                    _listPatients = value;
                    OnPropertyChanged("ListPatients");
                }
            }
        }

        public ObservableCollection<Dbo.User> ListUsers
        {
            get { return _listUsers; }
             set
            {
                if (_listUsers != value)
                {
                    _listUsers = value;
                    OnPropertyChanged("listUsers");
                }
            }
        }

        public object HasRights
        {
            get { return _hasRights; }
             set
            {
                if (_hasRights != value)
                {
                    _hasRights = value;
                    OnPropertyChanged("HasRights");
                }
            }
        }

        public string UserName
        {
            get { return _userName; }
             set
            {
                if (_userName != value)
                {
                    _userName = value;
                    OnPropertyChanged("UserName");
                }
            }
        }

        public ICommand LogoutCommand
        {
            get { return _logoutCommand; }
            set { _logoutCommand = value; }
        }

        public ICommand AddUserCommand
        {
            get { return _addUserCommand; }
            set { _addUserCommand = value; }
        }

        public ICommand AddPatientCommand
        {
            get { return _addPatientCommand; }
            set { _addPatientCommand = value; }
        }

        public ICommand DeletePatientCommand
        {
            get { return _deletePatientCommand; }
            set { _deletePatientCommand = value; }
        }

        public ICommand DeleteUserCommand
        {
            get { return _deleteUserCommand; }
            set { _deleteUserCommand = value; }
        }

        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }
        #endregion

        public HomeViewModel(string userLogin)
        {
            UserName = "Hello " + userLogin + "!";
            
            ServicePatient.ServicePatientClient pat = new ServicePatient.ServicePatientClient();
            ListPatients = new ObservableCollection<Dbo.Patient>(pat.GetListPatient());
            
            foreach (Dbo.Patient p in ListPatients)
            {
                
                ObservationPatient = p.Observations;
                foreach (Dbo.Observation o in ObservationPatient)
                {
                    string[] prescriptionarray = o.Prescription;                    
                    PrescriptionList = new System.Collections.ObjectModel.ObservableCollection<string>(prescriptionarray);
                }
            }
            ServiceUser.ServiceUserClient use = new ServiceUser.ServiceUserClient();
            ListUsers = new ObservableCollection<Dbo.User>(use.GetListUser());
            foreach (Dbo.User u in ListUsers)
            {
                if (!AvailablePictures.ContainsKey(u.Role))
                    AvailablePictures.Add(u.Role, u.Picture);
            }
            
            Dbo.User currentUser = use.GetUser(userLogin);
            Img = AvailablePictures["Medecin"];
            if (currentUser.Role != "Infirmière")
                HasRights = Visibility.Visible;
            else
                HasRights = Visibility.Collapsed;
            _logoutCommand = new RelayCommand(param => LogoutAccess(), param => true);
            _addUserCommand = new RelayCommand(param => AddUserAccess(), param => true);
            _deleteUserCommand = new RelayCommand(param => DeleteUserAccess(), param => true);
            _addPatientCommand = new RelayCommand(param => AddPatientAccess(), param => true);
            _deletePatientCommand = new RelayCommand(param => DeletePatientAccess(), param => true);
        }

        /// <summary>
        /// action permettant de se déconnecter
        /// </summary>
        private void LogoutAccess()
        {
            ServiceUser.IServiceUser user = new ServiceUser.ServiceUserClient();
                View.LoginView main = new View.LoginView();
                ViewModel.LoginViewModel vm = new LoginViewModel();
                main.DataContext = vm;
                App.Current.MainWindow = main;
                this.CloseSignal = true;
                main.Show();
        }

        private void AddUserAccess()
        {
            View.AddUser main = new View.AddUser();
            ViewModel.AddUserViewModel vm = new AddUserViewModel();
            vm.Hvm = this;
            main.DataContext = vm;
            App.Current.MainWindow = main;
            main.Show();
        }

        private void AddPatientAccess()
        {
            View.AddPatientView main = new View.AddPatientView();
            ViewModel.AddPatientViewModel vm = new AddPatientViewModel();
            vm.Hvm = this;
            main.DataContext = vm;
            App.Current.MainWindow = main;
            main.Show();
        }

        private void DeleteUserAccess()
        {
            View.DeleteUser main = new View.DeleteUser();
            ViewModel.DeleteUserViewModel vm = new DeleteUserViewModel();
            vm.Hvm = this;
            main.DataContext = vm;        
            App.Current.MainWindow = main;
            main.Show();
        }

        private void DeletePatientAccess()
        {
            View.DeletePatientView main = new View.DeletePatientView();
            ViewModel.DeletePatientViewModel vm = new DeletePatientViewModel();
            vm.Hvm = this;
            main.DataContext = vm;
            App.Current.MainWindow = main;
            main.Show();
        }
    }
}
